package com.zyx.wanshu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyx.wanshu.entity.User;
import com.zyx.wanshu.entity.UserRole;
import com.zyx.wanshu.entity.UserRoleVO;
import com.zyx.wanshu.mapper.UserMapper;
import com.zyx.wanshu.service.IUserRoleService;
import com.zyx.wanshu.service.IUserService;
import com.zyx.wanshu.util.Query;
import com.zyx.wanshu.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyx
 * @since 2022/7/15 22:25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IUserRoleService userRoleService;

    @Override
    public List<User> queryByUser(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (user != null) {
            // 根据账号查询相关信息
            queryWrapper.eq(StrUtil.isNotBlank(user.getUserName()), User::getUserName, user.getUserName());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<User> search(String key) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .like(StrUtil.isNotBlank(key), User::getUserName, key)
                .or().like(StrUtil.isNotBlank(key), User::getNickName, key)
                .list();
    }

    @Override
    public UserVO searchPageUser(UserVO vo) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(vo.getKey()), User::getUserName, vo.getKey())
                .or().like(StrUtil.isNotBlank(vo.getKey()), User::getNickName, vo.getKey())
                .orderByAsc(User::getOrderRank);
        IPage<User> page = this.page(new Query<User>().getPage(vo.getMap()),
                queryWrapper);
        vo.setPage(page);
        return vo;
    }

    /**
     * 判断账号是否存在
     */
    @Override
    public Boolean checkUserName(String userName) {
        return this.getBaseMapper().selectCount(new LambdaQueryWrapper<User>().eq(User::getUserName, userName)) == 0;
    }

    @Override
    public Boolean checkMobile(String mobile) {
        return this.getBaseMapper().selectCount(new LambdaQueryWrapper<User>().eq(User::getMobile, mobile)) == 0;
    }

    @Override
    public List<Integer> getRoleByUserId(Integer id) {
        List<UserRole> userRoleList = userRoleService
                .list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, id));
        if (CollectionUtil.isEmpty(userRoleList)) {
            return null;
        }
        return userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList());
    }

    /**
     * 给用户分配对应的角色
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void allocateRole(UserRoleVO vo) {
        // 对应的用户
        Integer userId = vo.getUserId();
        // 先删除已经分配的角色
        userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId));
        // 分配的角色
        List<Integer> roleIds = vo.getRoleIds();
        // 组合为操作的数据
        List<UserRole> list = roleIds.stream().map(item -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(item);
            return userRole;
        }).collect(Collectors.toList());
        // 批量保存
        userRoleService.saveBatch(list);
    }





}
